import { getOrderNetworkAddress, getWorkUserAddress } from '@/api/parts'
import { ref } from 'vue'
import {
  checkApplyedTicketOrderByOrderIds,
  checkCode,
  engineerEndOrderApply,
  engineerTakeMaterialConfirm,
  getEndOrderFieldList,
  getOrderStatusQty,
  getWaitCheckOrderEvent,
  saveEndOrderResult,
} from '@/api/order'

export const useGetOrderCustomerAddressByOrderId = () => {
  const getAddressLoading = ref(false)
  const address = ref<OrderCustomerAddress>({
    address: '',
    area: '',
    avatar: null,
    city: '',
    code: null,
    disabled: false,
    id: '',
    name: '',
    phone: '',
    province: '',
    qty: null,
    select: false,
    town: '',
    userName: null,
  })

  const getAddress = async (orderId: string) => {
    getAddressLoading.value = true
    const res = await getWorkUserAddress(orderId)
    address.value = res.data.data.row
    getAddressLoading.value = false
  }

  return {
    address,
    getAddressLoading,
    getAddress,
  }
}

export const useGetNetwordAddressByOrderId = () => {
  const getAddressLoading = ref(false)
  const address = ref({
    address: '',
    area: '',
    avatar: null,
    city: '',
    code: '',
    disabled: false,
    id: '',
    name: '',
    phone: '',
    province: '',
    qty: null,
    select: false,
    town: '',
    userName: null,
  })

  const getAddress = async (orderId: string) => {
    getAddressLoading.value = true
    const res = await getOrderNetworkAddress(orderId)
    address.value = res.data.data.row
    getAddressLoading.value = false
  }

  return {
    address,
    getAddressLoading,
    getAddress,
  }
}

/**
 * 获取待处理的工单事件
 */
export const useGetWaitHandleOrderEvent = () => {
  // 获取事件加载
  const getEventLoading = ref(false)

  // 待处理事件列表
  const waitHandleEventList = ref<Array<OrderWaitHandleEvent>>([])

  /**
   * 获取工单待处理的事件
   * @param {string} orderId - 工单id
   */
  const getWaitHandleEvent = async (orderId: string) => {
    getEventLoading.value = true
    const res = await getWaitCheckOrderEvent(orderId)
    getEventLoading.value = false
    if (res.data.code === 20000) {
      waitHandleEventList.value = res.data.data.rows
      return ''
    }
    return res.data.message
  }

  return {
    getEventLoading,
    waitHandleEventList,
    getWaitHandleEvent,
  }
}

/**
 * 审核序列号
 */
export const useCheckCode = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref({
    remark: '',
    checkResult: false,
  })

  /**
   * 审核
   * @param {string} orderId - 工单id
   */
  const check = async (orderId: string) => {
    checkLoading.value = true
    const res = await checkCode(checkForm.value, orderId)
    checkLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    checkForm.value = {
      remark: '',
      checkResult: false,
    }
  }

  return {
    checkForm,
    checkLoading,
    check,
    resetForm,
  }
}

/**
 * 获取工单列表每个状态对应的工单数量
 */
export const useGetOrderListStatusQty = () => {
  // 获取数量加载
  const getOrderListStatusQtyLoading = ref(false)

  // 每个状态对应的工单数量
  const orderListStatusQty = ref<OrderListStatusQty>({
    cancelOrder: 0,
    feeWaitCheckQty: 0,
    finishOrder: 0,
    handlerOrder: 0,
    notOpenTicketCount: 0,
    notPaymentStatusCount: 0,
    openTicketCount: 0,
    openTicketTotalCount: 0,
    paymentStatusCount: 0,
    paymentStatusTotalCount: 0,
    snCodeWaitCheckCount: 0,
    spareWaitCheckQty: 0,
    statementReview: 0,
    totalOrder: 0,
    waitCallPhoneQty: 0,
    waitDispatch: 0,
    waitEngineerReceivingOrderQty: 0,
    waitNetworkDispatchOrderQty: 0,
    waitNetworkHandlerOrder: 0,
    waitNetworkReceivingOrderQty: 0,
    waitReservationQty: 0,
    waitSignQty: 0,
    snCodeWarrantyDateWaitCheckCount: 0,
  })

  /**
   * 获取数量
   */
  const getOrderListStatusQty = async () => {
    getOrderListStatusQtyLoading.value = true
    const res = await getOrderStatusQty()
    getOrderListStatusQtyLoading.value = false
    if (res.data.code === 20000) {
      orderListStatusQty.value = res.data.data.row
      return ''
    }
    return res.data.message
  }

  return {
    orderListStatusQty,
    getOrderListStatusQtyLoading,
    getOrderListStatusQty,
  }
}

/**
 * 申请结单（部门角色使用）
 */
export const useEngineerEndOrderApply = () => {
  // 申请加载
  const applyLoading = ref(false)

  // 申请表单
  const applyForm = ref<OrderAPIRequest.EndOrderApply>({
    faultReason: [],
    faultRelationVos: [],
    faultTypeRelationVos: [],
    id: '',
    moduleId: '',
    remark: '',
    reviewRemark: '',
    solution: [],
    statementWriteOffCode: '',
    templateVos: [],
  })

  /**
   * 申请结单
   * @param {string} orderId - 工单id
   */
  const apply = async (orderId: string) => {
    applyLoading.value = true
    const res = await engineerEndOrderApply(applyForm.value, orderId)
    applyLoading.value = false
    return res.data
  }

  return {
    applyForm,
    applyLoading,
    apply,
  }
}

/**
 * 确定领料（部门角色使用）
 */
export const useEngineerTakeMaterialConfirm = () => {
  // 领料加载
  const takeMaterialLoading = ref(false)

  /**
   * 确认领料
   * @param {string} id - 工单id
   */
  const takeMaterial = async (id: string) => {
    takeMaterialLoading.value = true
    const res = await engineerTakeMaterialConfirm(id)
    takeMaterialLoading.value = false
    return res.data
  }

  return {
    takeMaterialLoading,
    takeMaterial,
  }
}

/**
 * 根据工单id审核已申请开票的工单
 */
export const useCheckInvoiceByIds = () => {
  // 审核加载
  const checkLoading = ref(false)

  /**
   * 审核
   * @param {Array<string>} ids - 工单id集合
   */
  const check = async (ids: Array<string>) => {
    checkLoading.value = true
    const res = await checkApplyedTicketOrderByOrderIds(ids)
    checkLoading.value = false
    return res.data
  }

  return {
    checkLoading,
    check,
  }
}

/**
 * 获取工单结单列表
 */
export const useGetOrderEndFieldList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 字段列表
  const fieldList = ref<OrderEndFieldListItem['objects']>([])

  /**
   * 获取字段列表
   * @param {string} id - 工单id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getEndOrderFieldList(id)
    getListLoading.value = false
    if (res.data.code === 20000) {
      fieldList.value = res.data.data.objects
      return ''
    }

    return res.data.message
  }

  /**
   * 重置字段列表
   */
  const resetFieldList = () => {
    fieldList.value.length = 0
  }

  return {
    fieldList,
    getListLoading,
    getList,
    resetFieldList,
  }
}

/**
 * 工单结单信息保存
 */
export const useSaveOrderEndResult = () => {
  // 保存加载
  const saveLoading = ref(false)

  // 保存表单
  const saveForm = ref<OrderEndFieldListItem['objects']>([])

  /**
   * 重置表单
   */
  const resetForm = () => {
    saveForm.value.length = 0
  }

  /**
   * 保存
   * @param {string} id - 工单id
   * @param {0|1|2|3} type - 保存类型，0->申请 1->审核通过 2->审核驳回 3->保存并审核
   * @param {string} moduleId - 工单模块id
   */
  const save = async (id: string, type: 0 | 1 | 2 | 3, moduleId: string) => {
    saveLoading.value = true
    const res = await saveEndOrderResult(id, type, moduleId, saveForm.value)
    saveLoading.value = false
    return res.data
  }

  return {
    saveForm,
    saveLoading,
    resetForm,
    save,
  }
}
